
clear all
cap log close

sysdir set PERSONAL "W:\ado"
set scheme kailascheme


*sysdir set PERSONAL "W:\ado"

*Defome dummie used in the eventstudies
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
*define fixed effects 
global fe = "baseid time year" 
*define clustering
global cluster = "sykstun"


capture program drop eventStudyGraphs
program define eventStudyGraphs
	args a b c d
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe )  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace coef_est = 0 if t == - 1
	replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	


	
	*Main regression for top guys 
	reghdfe `a'  treatPost ,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time since Violence") ), ///
		 legend( order( 2 "Nonworkplace" ) rows(2) position(7) ring(0) ) ///
		
	gr export "${results}\reg_nonworkplace_`a'.pdf", replace
	gr export "${results}\reg_nonworkplace_`a'.eps", replace
	
	* Save estimates 
	keep coef* se_est uC* lC* obs* t
	drop if missing(t)
	gen outcome = "`a'"

	restore
	}
end


use "${dataout}nonworkplace_firms_matched.dta", clear

sort match_id1 year_event time sykstun

bysort match_id1 year_event time: ereplace mm=max(mm)
bysort match_id1 year_event time: ereplace mf=max(mf)


gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1


*Time displacement dummies
g dpl_5=time==-5 & treat==1
g dpl_4=time==-4 & treat==1
g dpl_3=time==-3 & treat==1
g dpl_2=time==-2 & treat==1
g dpl_1=time==-1 & treat==1
g dpl_0=time==0 & treat==1
gen dpl1=time==1 & treat==1
gen dpl2=time==2 & treat==1
gen dpl3=time==3 & treat==1
gen dpl4=time==4 & treat==1
gen dpl5=time==5 & treat==1

gen treatPost= treat==1 & time>0


egen baseid=group(sykstun)



preserve 
keep if mm==1 
rename share_female share_female_mm
eventStudyGraphs "share_female_mm" "-0.04(0.02)0.04" "Share of female employees"
restore 

preserve 
keep if mf==1 
rename share_female share_female_mf
eventStudyGraphs "share_female_mf" "-0.04(0.02)0.04" "Share of female employees"
restore 

